package com.psychproject.web.services;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.psychproject.web.domains.User;
import com.psychproject.web.helpers.PageHelper;
import com.psychproject.web.helpers.PageResult;
import com.psychproject.web.mappers.HelperGroupMapper;
import com.psychproject.web.mappers.UserMapper;

@Service
@Transactional
public class UserServices {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private HelperGroupMapper helperGroupMapper;
	public void createUser(User user){
		userMapper.create(user);
	}

	public User findById(Long id) {
		return userMapper.findById(id);
	}

	public void delete(User user) {
		//先重置所有以该用户为领导者的帮助组
		helperGroupMapper.restLeader(user);
		userMapper.delete(user);
	}
	
	public List<User> all() {
		return userMapper.all();
	}

	public PageResult<User> paged(Integer page, String pageUrl) {
		int totalCount = userMapper.totalCount();
		PageHelper pageHelper = new PageHelper(page, totalCount,pageUrl);
		List<User> users =  userMapper.paged(pageHelper);
		return new PageResult<>(users,pageHelper);
	}

	public void update(User user) {
		userMapper.update(user);
	}
}
